package com.company.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.utils.RestUtil;
import com.company.project.pojo.Rest;
import com.company.project.pojo.ServiceCode;
import com.company.project.common.utils.JwtUtil;
import com.company.project.pojo.institution.Institution;
import com.company.project.pojo.institution.InstitutionDTO;
import com.company.project.pojo.roleVillage.RoleVillageDTO;
import com.company.project.pojo.village.Village;
import com.company.project.pojo.village.VillageDTO;
import com.company.project.pojo.village.VillageSelectVO;
import com.company.project.service.IVillageService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 村庄 前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2019-08-10
 */
@Api(value = "村庄",tags = "村庄")
@Controller
@RequestMapping("village")
public class VillageController {

    @Autowired
    HttpServletRequest request;
    @Autowired
    private IVillageService iVillageService;

    @ApiOperation(value = "获取村庄列表")
    @RequestMapping(value = "getVillageList", method = RequestMethod.GET)
    @ResponseBody
    public Rest getVillageList(){
        QueryWrapper<Village> queryWrapper = new QueryWrapper<>();

        List villageList = iVillageService.list(queryWrapper);

        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！" , villageList);
    }

    @ApiOperation(value = "根据单位id获取村庄列表分页")
    @RequestMapping(value = "getVillageListByInstitutionIdPage", method = RequestMethod.GET)
    @ResponseBody
    public Rest getVillageListByInstitutionIdPage(int institutionId, int currentPage){
        QueryWrapper<Village> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("institution_id", institutionId);
        queryWrapper.eq("is_delete", 0);

        IPage page = iVillageService.page(new Page(currentPage, 15), queryWrapper);

        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！" , page);
    }

    @ApiOperation(value = "根据单位id获取村庄列表")
    @RequestMapping(value = "getVillageListByInstitutionId", method = RequestMethod.GET)
    @ResponseBody
    public Rest getVillageListByInstitutionId(Integer institutionId){
        List<VillageDTO> villageList = iVillageService.getVillageListByInstitutionId(institutionId);

        List<VillageSelectVO> villageSelectVOList = new ArrayList<>();
        for (VillageDTO villageDTO: villageList) {
            VillageSelectVO villageSelectVO = new VillageSelectVO();
            BeanUtils.copyProperties(villageDTO, villageSelectVO);

            villageSelectVOList.add(villageSelectVO);
        }

        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！" , villageSelectVOList);
    }

    @ApiOperation(value = "根据管理区id获取登陆人管理村庄列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", value = "管理区id", name = "institutionId", required = true, dataType = "int")
    })
    @RequestMapping(value = "getLoginUserVillageListByInstitutionId", method = RequestMethod.GET)
    @ResponseBody
    public Rest<VillageSelectVO> getLoginUserVillageListByInstitutionId(Integer institutionId){
        Claims claims = JwtUtil.parseJWT(request.getHeader("token"));
        Integer role_id = claims.get("role_id", Integer.class);

        // 根据角色id查询村庄
        List<VillageDTO> villageDTOList = iVillageService.getVillageListByInstitutionIdAndRoleId(role_id, institutionId);

        List<VillageSelectVO> villageSelectVOList = new ArrayList<>();
        for (VillageDTO villageDTO: villageDTOList){
            VillageSelectVO villageSelectVO = new VillageSelectVO();
            BeanUtils.copyProperties(villageDTO, villageSelectVO);

            villageSelectVOList.add(villageSelectVO);
        }

        return RestUtil.rest(ServiceCode.CODE_200, "成功" , villageSelectVOList);
    }

    @ApiOperation(value = "添加村庄")
    @RequestMapping(value = "addVillage", method = RequestMethod.POST)
    @ResponseBody
    public Rest addInstitution(@RequestBody Village village){
        QueryWrapper<Village> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", village.getName());
        queryWrapper.eq("is_delete", 0);
        List<Village> villageList = iVillageService.list(queryWrapper);
        if(villageList.size() > 0){
            return RestUtil.rest(ServiceCode.CODE_403, "该村庄已存在");
        }

        Claims claims = JwtUtil.parseJWT(request.getHeader("token"));
        Integer user_id = claims.get("user_id", Integer.class);

        village.setInputUserId(user_id);
        iVillageService.save(village);


        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！");
    }

    @ApiOperation(value = "修改村庄")
    @RequestMapping(value = "updateVillage", method = RequestMethod.POST)
    @ResponseBody
    public Rest updateVillage(@RequestBody Village village){
        QueryWrapper<Village> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", village.getName());
        queryWrapper.eq("is_delete", 0);
        List<Village> villageList = iVillageService.list(queryWrapper);
        for (int i=0; i<villageList.size(); i++){
            if (villageList.get(i).getId().equals(village.getId())){
                continue;
            }
            return RestUtil.rest(ServiceCode.CODE_403, "该村庄已存在");
        }

        iVillageService.updateById(village);

        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！");
    }

    @ApiOperation(value = "删除村庄")
    @RequestMapping(value = "deleteVillage", method = RequestMethod.POST)
    @ResponseBody
    public Rest deleteVillage(Integer id){
        Village village = new Village();
        village.setId(id);
        village.setIsDelete(1);

        iVillageService.updateById(village);

        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！");
    }

}

